import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);

const store = new Vuex.Store({
  state: {
    error: null,
    info: null
  },
  mutations: {
    setError(state, message) {
      state.error = message;
    },

    setInfo(state, message) {
      state.info = message;
    },

    clearError(state) {
      state.error = null;
    },

    clearInfo(state) {
      state.info = null;
    }
  },

  actions: {
    putError(context, message) {
      context.commit('setError', message);
      setTimeout(() => {
        context.commit('clearError')
      }, 3000)
    },

    putInfo(context, message) {
      context.commit('setInfo', message);
      setTimeout(() => {
        context.commit('clearInfo')
      }, 3000)
    }
  },

  getters: {
    error(state) {
      return state.error;
    },
    info(state) {
      return state.info;
    }
  }
});

export default store;
